package emulator.shell.GUI.symbols;

import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableModel;

import emulator.shell.GUI.util.DialogBase;

@SuppressWarnings("serial")
public class SaveSymbolsDialog extends DialogBase
{
	private JTextField fileName;
	public String getFileName()
	{
		return fileName.getText();
	}
	
	public SaveSymbolsDialog(JFrame owner, TableModel tableModel)
	{
		super(owner, "Save symbols", true);
		buildContents(tableModel);
		pack();
		setLocationRelativeTo(null);
	}

	private void buildContents(TableModel tableModel)
	{
		getContentPane().setLayout(new BorderLayout());
		
		fileName = new JTextField();
		fileName.setText("labels.sym");
		setMargins(fileName, 5);
		JButton buttonBrowse = new JButton("Browse...");
		buttonBrowse.addActionListener(new OnBrowse());
		setMargins(buttonBrowse, 5);
		
		JPanel filePanel = new JPanel();
		filePanel.setLayout(new BorderLayout());
		filePanel.add(fileName, BorderLayout.CENTER);
		filePanel.add(buttonBrowse, BorderLayout.EAST);
		
		JTable sourceTable = new JTable();
		sourceTable.setAutoCreateRowSorter(true);
		sourceTable.setModel(tableModel);
	
		add(filePanel, BorderLayout.NORTH);
		add(new JScrollPane(sourceTable), BorderLayout.CENTER);
		add(createButtonPanel(), BorderLayout.SOUTH);
	}

	public class OnBrowse implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			FileDialog fileDialog = new FileDialog(SaveSymbolsDialog.this, "Select file name for symbols", FileDialog.SAVE);
			fileDialog.setFile(fileName.getText());
			fileDialog.setVisible(true);
			if (fileDialog.getDirectory() != null && fileDialog.getFile() != null)
				fileName.setText(fileDialog.getDirectory() + fileDialog.getFile());
		}

	}
}
